package com.wolfword.crawler.server.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.wolfword.common.util.object.StringUtil;
import com.wolfword.common.util.password.MD5Util;
import com.wolfword.crawler.server.entity.UserInfoEntity;
import com.wolfword.crawler.server.service.LoginService;

@Service("LoginServiceImpl")
public class LoginServiceImpl implements LoginService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 没错，我偷懒了，这里没有使用mybati构架，仅此一处
	 * @param loginName
	 * @param password
	 * @return
	 * @author ServerZhang
	 * @date 2018年1月25日
	 */
	@Override
	public UserInfoEntity verify(String loginName, String password){
		UserInfoEntity entity = new UserInfoEntity();
		if (StringUtil.isEmpty(loginName,password)) {
			return null;
		}
		String encrypt = MD5Util.encrypt(password);
		String sql = "Select * from user_info where login_name='%s' and password='%s' limit 1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, loginName,encrypt));
		if (list.size() < 1) {
			return null;
		}
		Map<String, Object> map = list.get(0);
		if (map == null) {
			return null;
		}
		entity.setId(Long.parseLong(map.get("id").toString()));
		entity.setLoginName(loginName);
		Object userNameObj = map.get("user_name");
		if (userNameObj != null) {
			entity.setUserName(userNameObj.toString());
		}
		return entity;
	}
	
}
